package com.hlm.web.controller;

import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.bouncycastle.cms.CMSSignatureAlgorithmNameGenerator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
//这个是用的jjwt包
@RestController
public class TokenLoginController {
    private String token;
    @GetMapping("/login")
    public void login(String userName , String password){
        Map<String,String> map = new HashMap<>();
        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
        System.out.println(signatureAlgorithm+"算法");
        try{
             System.out.println(Jwts.builder().setSubject("sss").signWith(signatureAlgorithm,"fffck"));
             token = Jwts.builder().setSubject("sss") //主题，可以放用户的详细信息
                    .setIssuedAt(new Date()) //token创建时间
                    .setExpiration(new Date(System.currentTimeMillis() + 60000)) //token过期时间60s
                    .setId("userId")//用户ID
                    .signWith(signatureAlgorithm,"fffck")
                    .compact();
              System.out.println("token:"+token);
              map.put("code","1");
              map.put("msg","success");
              map.put("token",token);
              map.put("user",userName);
              System.out.println(map);
        }catch (Exception e){
            map.put("code","0");
            map.put("msg","fail");
            e.printStackTrace();
        }
    }

    public void check(){
        JwtParser jwtParser = Jwts.parser();
        jwtParser.parseClaimsJws(token).getBody().getSubject();
    }
}
